﻿function Favorite() {

    //var consumerId = "34400207";
    this.Options = {
        url: RSECGlobal.ProductSite + "Goods/Services/ItemFavorite.aspx",
        data: null,
        type: "GET",
        dataType: "json",
        loginUrl: ('https:' == document.location.protocol ? RSECGlobal.MemberSite.replace("http:", "https:") : RSECGlobal.MemberSite) + "Customer/ConsumerLogin.aspx",
        loginReturnUrl: location.href,
        itemId: null,
        action: {
            validateLogin: "validateLogin",
            add: "add",
            addClass: "addClass",
            setClass: "setfavoriteclass"
        },
        controls: {
            divContainer: "#div_ItemFavorite",
            divBackgroud: "#below",
            selFavoriteClass: "#FavoriteClass",
            spanFavoriteCount: "#spanFavoriteCount",
            divClassInput: "#InputPanel",
            remark: "#Remark",
            className: "#className"
        },
        favoriteId: 0,
        clickEvent: null
    };

    var width = 328;
    var height = 282;
    var left = $(window).width() / 2 - width / 2;
    var top = $(window).height() / 2 - height;
    $(this.Options.controls.divContainer).css("left", left);
    $(this.Options.controls.divContainer).css("top", top);

}

Favorite.prototype = new RSUIBase();

//    $(document).mousemove(function (e) {
//        favorite.Options.clickEvent = e;
//    });

Favorite.prototype.init = function (e) {
    $(this.Options.controls.divClassInput).hide();
    $(this.Options.controls.remark).val("");
}

Favorite.prototype.show = function (itemId) {
    this.init();
    this.Options.itemId = itemId;
    var consumerId = $("#hidConsumerId").val();
    if (consumerId == undefined || consumerId == null) {
        consumerId = "";
    }
    $.webPageForJson(this.Options.url, { action: this.Options.action.validateLogin, ConsumerId: consumerId }, $.proxy(function (callBack) {
        if (callBack.IsSuccess) {
            if (!callBack.Data.success) {
                if (callBack.Data.isNeedLogin) {
                    this.login();
                    return false;
                }

                WinAlert("提示信息", callBack.Data.msg);
                return;
            }
            var v_left = ($(window).width() - $("#divShowFavorite").width()) / 2;
            var v_top = ($(window).height() - $("#divShowFavorite").height()) / 2;
            var element = document.body || document.body;
            v_left += $(document).scrollLeft();
            v_top += $(document).scrollTop();
            $("#divShowFavorite").css("position", "absolute");
            $("#divShowFavorite").css("left", v_left + "px");
            $("#divShowFavorite").css("top", v_top + "px");
            $("#divShowFavorite").css("z-index", 1000);
            $("#divShowFavorite").hide().show();

            if (callBack.Data.classDatas.length != 0)
                $(this.Options.controls.selFavoriteClass).find("option").remove();
            $.each(callBack.Data.classDatas, $.proxy(function (i, item) {
                $(this.Options.controls.selFavoriteClass).append("<option value=\"" + item.classId + "\">" + item.className + "</option>")
            }, this));
            this.add(itemId);
        }
        else
            alert(callBack.ErrorInfo);
    }, this));
}


Favorite.prototype.login = function (itemId) {
    location.href = this.Options.loginUrl + "?returnUrl=" + escape(this.Options.loginReturnUrl);
}


Favorite.prototype.add = function (itemId) {
    var consumerId = $("#hidConsumerId").val();
    if (consumerId == undefined || consumerId == null) {
        consumerId = "";
    }
    $.webPageForJson(this.Options.url, { action: this.Options.action.add, itemId: itemId, ConsumerId: consumerId }, $.proxy(function (callBack) {
        if (callBack.IsSuccess) {
            if (!callBack.Data.success) {
                if (callBack.Data.isNeedLogin) {
                    this.login();
                    return false;
                }

                WinAlert("提示信息", callBack.Data.msg);
                return;
            }

            $(this.Options.controls.spanFavoriteCount).html(callBack.Data.favoriteCount);
            this.Options.favoriteId = callBack.Data.favoriteId;
        }
        else
            alert(callBack.ErrorInfo);
    }, this));
}


Favorite.prototype.addClass = function () {
    var className = $("#className").val();
    var remiderType = $("#remiderType").val();
    var remiderVlaue = $("#remiderVlaue").val();
    var consumerId = $("#hidConsumerId").val();
    if (consumerId == undefined || consumerId == null) {
        consumerId = "";
    }
    $.webPageForJson(this.Options.url,
        {
            action: this.Options.action.addClass,
            className: className,
            remiderType: remiderType,
            remiderValue: remiderVlaue,
            ConsumerId: consumerId
        }, $.proxy(function (callBack) {
            if (callBack.IsSuccess) {
                if (!callBack.Data.success) {
                    if (callBack.Data.isNeedLogin) {
                        this.login();
                        return false;
                    }

                    WinAlert("提示信息", callBack.Data.msg);
                    return;
                    //                        new MsgBox(callBack.Data.msg, "提示信息", { showLogin: callBack.Data.isNeedLogin });
                    //                        return;
                }
                $(this.Options.controls.selFavoriteClass).append("<option selected=\"true\" value=\"" + callBack.Data.classId + "\">" + callBack.Data.className + "</option>");
                this.hideClass();

            }
            else
                alert(callBack.ErrorInfo);
        }, this));
}

Favorite.prototype.SetClass = function () {
    var classId = $(this.Options.controls.selFavoriteClass).val();
    var consumerId = $("#hidConsumerId").val();
    if (consumerId == undefined || consumerId == null) {
        consumerId = "";
    }
    $.webPageForJson(this.Options.url,
    { action: this.Options.action.setClass, classId: classId, favoriteId: this.Options.favoriteId, ConsumerId: consumerId }, $.proxy(function (callBack) {
        if (callBack.IsSuccess) {
            if (!callBack.Data.success) {
                if (callBack.Data.isNeedLogin) {
                    this.login();
                    return false;
                }

                WinAlert("提示信息", callBack.Data.msg);
                return;
            }
            //new MsgBox("保存成功", "温馨提示", { showLogin: false });
            this.hide();
        }
        else {
            alert(callBack.ErrorInfo);
        }

    }, this));
}


Favorite.prototype.showClass = function () {
    var v_left = ($(window).width() - $("#InputPanel").width()) / 2;
    var v_top = ($(window).height() - $("#InputPanel").height()) / 2;
    var element = document.body || document.body;
    v_left += $(document).scrollLeft();
    v_top += $(document).scrollTop();
    $("#InputPanel").css("position", "absolute");
    $("#InputPanel").css("left", v_left + "px");
    $("#InputPanel").css("top", v_top + "px");
    $("#InputPanel").css("z-index", 1001);
    $("#InputPanel").hide().show();
    $(this.Options.controls.className).val("新的分类");
}

Favorite.prototype.hideClass = function () {
    $(this.Options.controls.divClassInput).hide();
}


Favorite.prototype.hide = function () {
    $("#InputPanel").hide();
    $("#divShowFavorite").hide();
}

var favorite = new Favorite();
